//
//  HomeView.swift
//  loveHHEveryDay
//
//  Created by yw on 2025/4/2.
//

import SwiftUI

struct HomeView: View {
    @AppStorage("user_bg") var bg:String = ""
    @AppStorage(kUserDateIcon) var icon:String = ""
    @AppStorage(kUserBindId) var userBindId:String = ""
    @AppStorage(kUserIconBase64) var iconBase64:String = ""
    @State var currentBg:String = "bg_1"
    @StateObject var pathManager:PathManager = PathManager()
    @State var showTips:Bool = true
    
    @State var list:[HomeMenuModel] = [
        HomeMenuModel(title: "纪念日", icon: "home_calendar", subTitle: "记录下专属你们的纪念日"),
        HomeMenuModel(title: "反省中", icon: "home_write", subTitle: "记录下专属你们的纪念日"),
        HomeMenuModel(title: "ta的喜好", icon: "home_good", subTitle: "记录下专属你们的纪念日"),
        HomeMenuModel(title: "ta的雷点", icon: "home_bad", subTitle: "记录下专属你们的纪念日"),
    ]
    @State var gridItems:[GridItem] = [
        GridItem(.fixed(110), spacing: 16, alignment: .center),
        GridItem(.fixed(110), spacing: 16, alignment: .center),
//        GridItem(.fixed(110), spacing: 16, alignment: .center),
    ]
    
    @State var showClickAni:Bool = false
    @State var currentName:String = ""
    var body: some View {
        NavigationStack(path: $pathManager.path) {
            VStack {
                ScrollView(showsIndicators: false) {
                    ZStack(alignment: .topLeading) {
                        Image(currentBg)
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(maxWidth: kScreenWidth)
                        HStack {
                            bindView.offset(x: 20, y: 60)
                            Spacer()
                            changeImageView
                            
                        }.frame(maxWidth: .infinity)
                        if userBindId.isEmpty && showTips {
                            tipsView.offset(x:32,y:140)
                        }
                        menuView
                            .offset(y: 200)
                    }
                }
            }.navigationTitle("首页")
                .frame(maxWidth: .infinity)
                .navigationBarTitleDisplayMode(.inline)
                .background(Color("color_bg"))
                .onAppear {
                    if bg.hasPrefix("change_") {
                        let result = String(bg.dropFirst("change_".count))
                        currentBg = result
                    } else {
                        currentBg = "bg_1"
                    }
                    
                }
                .navigationDestination(for: String.self) { item in
                    if item == "用户绑定" {
                        BindUserIdView()
                    } else if item == "修改背景" {
                        ChangeBackgroundView()
                    } else if item == "纪念日" {
                        AnniversaryView()
                    }
                }
        }.environmentObject(pathManager)
        
    }
    
    var bindView:some View {
        ZStack {
            Image(uiImage: ((iconBase64.imageFromBase64) ?? UIImage(named: "wel_boy"))!)
                .resizable()
                .aspectRatio(contentMode: .fill)
                .clipShape(Circle())
                .frame(width: 64, height: 64)
                .padding(4)
                .background(.white)
                .clipShape(Circle())
            
            if userBindId.isEmpty {
                
                Image("home_add_id")
                    .resizable()
                    .aspectRatio(contentMode: .fill)
                    .frame(width: 36, height: 36)
                    .foregroundColor(.colorMain)
                    .clipShape(Circle())
                    .padding(4)
                    .frame(width: 64, height: 64)
                    .background(.white)
                    .clipShape(Circle())
                    .offset(x: 64)
                    .shadow(color: .colorWelBg02.opacity(0.6), radius: 4)
                //                    .overlay(alignment: .bottomTrailing, content: {
                
                //                    })
                    .shakeTapGes {
                        pathManager.path.append("用户绑定")
                    }
                
            } else {
                Image("截屏2025-04-22 08.17.38")
                    .resizable()
                    .aspectRatio(contentMode: .fill)
                    .clipShape(Circle())
                    .frame(width: 72, height: 72)
                    .padding(4)
                    .background(.white)
                    .clipShape(Circle())
                    .offset(x: 72)
                    .shadow(color: .colorWelBg02.opacity(0.6), radius: 4)
            }
            
        }
    }
    
    var changeImageView:some View {
        Image("home_change_img")
            .resizable()
            .aspectRatio(contentMode: .fit)
            .foregroundColor(.colorPink01)
            .frame(width: 30)
            .padding(10)
            .background(.white)
            .clipShape(Circle())
            .shadow(color: .colorWelBg02.opacity(0.6), radius: 4)
            .offset(x:-20,y:10)
            .shakeTapGes {
                pathManager.path.append("修改背景")
            }
    }
    
    var tipsView:some View {
        VStack(alignment: .leading, spacing: 0) {
            TalkTail().fill(.colorMain)
                .frame(width: 22, height: 8)
                .padding(.leading, 80)
            
            Text("立即添加我的另一半")
                .frame(width: 174,alignment: .leading)
                .lineLimit(1)
                .padding(6)
                .foregroundColor(.white)
                .background(.colorMain)
                .cornerRadius(12, antialiased: true)
        }
        .overlay(alignment: .trailing) {
            Image(systemName: "xmark")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .foregroundColor(.white)
                .frame(width: 12, height: 12)
            
                .offset(x: -8, y: 3)
                .shakeTapGes {
                    withAnimation {
                        showTips.toggle()
                    }
                }
        }
    }
    
    var menuView:some View {
        VStack(alignment: .center) {
            ForEach(list) { item in
                HStack {
                    HStack {
                        Image(item.icon)
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(width: 28, height: 28)
                            .padding(.trailing,10)
                        VStack(alignment: .leading) {
                            Text(item.title)
                                .modifier(YWTextModifer(fontSize: 16, fontWeight: .bold, design: .rounded, color: .colorTextMain))
    //                            .padding(.top,4)
                            Spacer()
                            Text(item.subTitle)
                                .modifier(YWTextModifer(fontSize: 12, fontWeight: .thin, design: .rounded, color: .colorTextMain))
    //                            .padding(.bottom,4)
                        }
                    }
                    Spacer()
                    Image(systemName: "arrow.right")
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                        .frame(width: 12, height: 12)
                        .foregroundColor(.colorWelBg02)
                }
                .padding()
                .frame(width: kScreenWidth - 40,height: 80)
                    .background(.white)
                    .cornerRadius(20)
                    .padding(.leading,20)
                    .padding(.vertical,6)
//                    .scaleEffect( currentName == item.title ? 1.02:1)
//                    .shadow(color: currentName == item.title ? .colorWelBg02:.clear, radius: 2)
                    .shakeTapGes {
                        
                        meneClick(item: item)
                    }
            }
        }
    }
    
    private func meneClick(item:HomeMenuModel) {
//        withAnimation {
//            currentName = item.title
//        }
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
            pathManager.path.append(item.title)
        }

    }
}
